package zhoug.zui.round;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

import zhoug.logging.Logger;
import zhoug.zui.R;
import zhoug.zui.alpha.ZUIAlphaImageView;
import zhoug.zui.utils.ZUIViewHelper;

/**
 *圆形图片 :裁剪路径实现,
 *优点:无黑色背景的bug
 *缺点:毛边
 * @Author 35574
 * @Date 2020/7/24
 * @Description
 */
public class ZUIRoundImageView1 extends ZUIAlphaImageView {
    private static final String TAG = ">>>ZUIRoundImageView1";
    private static  boolean debug = false;

    private ZUIViewHelper _mRoundViewHelper;

    public static void setDebug(boolean debug) {
        ZUIRoundImageView1.debug = debug;
    }

    public ZUIRoundImageView1(Context context) {
        this(context, null);
    }

    public ZUIRoundImageView1(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.ZUIRoundImageView_theme);
    }

    public ZUIRoundImageView1(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, R.style.ZUIRoundImageView_def);
    }

    public ZUIRoundImageView1(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        _getZUIViewHelper().parseAttribute(context, attrs, defStyleAttr, defStyleRes);
       if(isRound()){
           setScaleType(ScaleType.CENTER_CROP);
       }
    }

    private ZUIViewHelper _getZUIViewHelper(){
        if(_mRoundViewHelper==null){
            _mRoundViewHelper = new ZUIViewHelper(this);
        }
        return _mRoundViewHelper;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (debug) {
            Logger.d(TAG, "onSizeChanged:w="+w+",h="+h);
        }
        _getZUIViewHelper().onSizeChanged(w, h);
    }


    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);
        _getZUIViewHelper().onPressedChanged(this,pressed);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        _getZUIViewHelper().clipCanvas(canvas);
        super.onDraw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (debug) {
            Logger.d(TAG, "onMeasure:width="+width+",height="+height);
            Logger.d(TAG, "onMeasure:MeasuredWidth="+getMeasuredWidth()+",MeasuredHeight="+getMeasuredHeight());
        }
        int size = _getZUIViewHelper().measureSize(getMeasuredWidth(), getMeasuredHeight());
        if (size > 0) {
            setMeasuredDimension(size, size);
        }
    }

    public boolean isRound() {
        return   _getZUIViewHelper().isRound();
    }

    public void setRound(boolean round) {
        _getZUIViewHelper().setRound(round);
    }

    public void setCorners(int corners) {
        _getZUIViewHelper().setCorners(corners);
    }

    public int getCorners() {
        return   _getZUIViewHelper().getCorners();
    }

    public int getBorder() {
        return   _getZUIViewHelper().getBorder();
    }

    public void setBorder(int border) {
        _getZUIViewHelper().setCorners(border);
    }

    public int getBorderColor() {
        return   _getZUIViewHelper().getBorderColor();
    }

    public void setBorderColor(int borderColor) {
        _getZUIViewHelper().setBorderColor(borderColor);
    }

}
